package cn.windy.samary.module.admin.service.impl;

import cn.windy.samary.module.admin.common.UserLine;
import cn.windy.samary.module.admin.common.UserOnline;
import cn.windy.samary.module.admin.service.ISessionService;
import cn.windy.samary.utils.ShiroUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author zyl
 * @date 2018-06-06 10:58
 */
@Service
public class SessionServiceImpl implements ISessionService {

    private SessionDAO sessionDAO;

    @Autowired
    public SessionServiceImpl(SessionDAO sessionDAO) {
        this.sessionDAO = sessionDAO;
    }

    @Override
    public List<UserOnline> list() {
        List<UserOnline> list = new ArrayList<>();
        Collection<Session> sessions = sessionDAO.getActiveSessions();
        UserLine userLine = ShiroUtils.getUserLine();
        for (Session session : sessions) {
            UserOnline userOnline = new UserOnline();
            Object attribute = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            if (attribute != null) {
                SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) attribute;
                UserLine userDO = (UserLine) principalCollection.getPrimaryPrincipal();
                if (!"0001".equals(userLine.getId()) && "0001".equals(userDO.getId())) {
                    continue;
                }
                userOnline.setUsername(userDO.getUsername());
                userOnline.setNickname(userDO.getNickname());
                userOnline.setId((String) session.getId());
                userOnline.setHost(session.getHost());
                userOnline.setStartTimestamp(session.getStartTimestamp());
                userOnline.setLastAccessTime(session.getLastAccessTime());
                userOnline.setTimeout(new Date(session.getLastAccessTime().getTime() + session.getTimeout()));
                list.add(userOnline);
            }
        }
        return list;
    }

    @Override
    public List<UserLine> listOnlineUser() {
        List<UserLine> list = new ArrayList<>();
        Collection<Session> sessions = sessionDAO.getActiveSessions();
        for (Session session : sessions) {
            SimplePrincipalCollection principalCollection = new SimplePrincipalCollection();
            Object attribute = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            if (attribute != null) {
                principalCollection = (SimplePrincipalCollection) attribute;
                UserLine user = (UserLine) principalCollection.getPrimaryPrincipal();
                list.add(user);
            }
        }
        return list;
    }

    @Override
    public Collection<Session> sessionList() {
        return sessionDAO.getActiveSessions();
    }

    /**
     * 强制下线
     *
     * @param sessionId sessionId
     * @return boolean
     */
    @Override
    public boolean forceLogout(String sessionId) {
        Session session = sessionDAO.readSession(sessionId);
        session.setTimeout(0);
        return true;
    }

    @Override
    public List<Session> listActiveSession() {
        List<Session> list = new ArrayList<>();
        for (Session session : sessionList()) {
            if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) != null) {
                list.add(session);
            }
        }
        return list;
    }
}
